package com.czd.support.util;


//import org.apache.commons.logging.Log;
//import org.apache.commons.logging.LogFactory;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class GenericsUtil {
//	private static final Log log = LogFactory.getLog(GenericsUtil.class);
	
    private GenericsUtil() {  
    }  
     
    @SuppressWarnings("rawtypes")
	public static Class getSuperClassGenricType(Class clazz) {  
        return getSuperClassGenricType(clazz, 0);  
    }  
  
   
    @SuppressWarnings("rawtypes")
	public static Class getSuperClassGenricType(Class clazz, int index) {  
  
        Type genType = clazz.getGenericSuperclass();  
  
        if (!(genType instanceof ParameterizedType)) {  
//            log.warn(clazz.getSimpleName() + "'s superclass not ParameterizedType");
            return Object.class;  
        }  
  
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();  
  
        if (index >= params.length || index < 0) {  
//            log.warn("Index: " + index + ", Size of " + clazz.getSimpleName() + "'s Parameterized Type: "
//                    + params.length);
            return Object.class;  
        }  
        if (!(params[index] instanceof Class)) {  
//            log.warn(clazz.getSimpleName() + " not set the actual class on superclass generic parameter");
            return Object.class;  
        }  
        return (Class) params[index];  
    }

    /**
     * 获取客户端访问的ip地址
     * @param request
     * @return
     */
    public static String getIpAddr(HttpServletRequest request) {        
        String ip = request.getHeader("x-forwarded-for");        
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        
            ip = request.getHeader("Proxy-Client-IP");        
        }        
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        
            ip = request.getHeader("WL-Proxy-Client-IP");        
        }        
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {        
           ip = request.getRemoteAddr();        
       }        
       return ip;        
   }

    /**
     * 获取客户端访问的终端设备信息
     * @param request
     * @return
     */
   public static String getUserAgent(HttpServletRequest request){
      return  request.getHeader("user-agent");
   }

    /**
     * 获取客户端访问的信息来源
     * @param request
     * @return
     */
   public static String getReferer(HttpServletRequest request){
        return request.getHeader("referer");
   }

    /**
     * 获取客户端访问的当前url
     * @param request
     * @return
     */
   public static String getCurrentUrl(HttpServletRequest request){
        return request.getRequestURL().toString();
   }

   public static String getRequestURI(HttpServletRequest request){
       return request.getRequestURI();
   }
}
